package com.zmx.patientend.view.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.wang.adapters.interfaceabstract.OnItemClickListener;
import com.zmx.patientend.BaseActivity;
import com.zmx.patientend.R;
import com.zmx.patientend.adapter.MakeAppointmentAdapter;
import com.zmx.patientend.fragment.HomeFragment;
import com.zmx.patientend.okhttp.OkHttp3ClientManager;
import com.zmx.patientend.pojo.HospMessage;
import com.zmx.patientend.utils.AllUtil;
import com.zmx.patientend.view.util.CustomToast;
import com.zmx.patientend.view.util.MySharedPreferences;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 时间：2020/4/30 21:01
 * 功能：预约挂号选择医院
 */
public class MakeAppointmentActivity extends BaseActivity {

    private RecyclerView reView;
    private SmartRefreshLayout refreshLayout;
    private MakeAppointmentAdapter adapter;
    private List<HospMessage> lists;

    private View mEmptyLayout;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_make_appointment;
    }

    @Override
    protected void initViews() {

        setToolbar(R.id.tool_bar);

        mEmptyLayout = findViewById(R.id.empty);
        reView = findViewById(R.id.reView);
        reView.setLayoutManager(new LinearLayoutManager(mActivity));
        adapter = new MakeAppointmentAdapter(mActivity);
        reView.setAdapter(adapter);
        lists = new ArrayList<>();
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            protected void onItemClick(View view, int listPosition) {

                Bundle bundle = new Bundle();
                bundle.putString("hosp_id",lists.get(listPosition).getId()+"");
                startActivity(MakeAppointmentNextActivity.class,bundle);

            }
        });

        // 开启自动加载功能
        refreshLayout = findViewById(R.id.refreshLayout);
        refreshLayout.setEnableAutoLoadMore(true);
        refreshLayout.setOnRefreshListener(refreshLayout -> {

            lists.clear();
            getAllDoctor();

        });

        refreshLayout.setOnLoadMoreListener(refreshLayout -> {

            refreshLayout.finishLoadMore(true);
            CustomToast.getInstance().showToastCustom(mActivity, "没有更多数据啦!", Gravity.BOTTOM);

        });

        //触发自动刷新
        refreshLayout.autoRefresh();

    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler(){

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            switch (msg.what){

                case 1:
                    Log.e("数据","数据"+msg.obj.toString());

                    try {

                        JSONArray array = new JSONArray(msg.obj.toString());
                        Gson gson = new Gson();
                        for (int i=0;i<array.length();i++){

                            HospMessage hm = gson.fromJson(array.getJSONObject(i).toString(),HospMessage.class);
                            lists.add(hm);

                        }

                        adapter.setListAndNotifyDataSetChanged(lists);//就是设置list然后刷新
                        refreshLayout.finishRefresh(true);
                        if(lists.size()>0){
                            mEmptyLayout.setVisibility(View.GONE);
                        }else{
                            mEmptyLayout.setVisibility(View.VISIBLE);
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    break;

            }

        }
    };

    /**
     * 获取所有医院
     */
    public void getAllDoctor() {

        //请求网络
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sign", AllUtil.md5(sing(null)));
        params.put("token", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.token, ""));
        params.put("uid", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        params.put("ts", System.currentTimeMillis() / 1000); // 当前时间戳
        params.put("cmd", "getHosp");
        params.put("data[]", "");
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/app/", params, handler, 1, 404);

    }


}
